package com.ruoyi.student_club.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.student_club.constants.State;
import com.ruoyi.student_club.dto.LeaveMessageDto;
import com.ruoyi.student_club.vo.app.FirstMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student_club.mapper.FirstMessageMapper;
import com.ruoyi.student_club.domain.FirstMessage;
import com.ruoyi.student_club.service.IFirstMessageService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 留言管理Service业务层处理
 * 
 * @author 无尤
 * @date 2022-01-14
 */
@Service
public class FirstMessageServiceImpl implements IFirstMessageService 
{
    @Resource
    private FirstMessageMapper firstMessageMapper;

    /**
     * 查询留言管理
     * 
     * @param id 留言管理主键
     * @return 留言管理
     */
    @Override
    public FirstMessage selectFirstMessageById(Long id)
    {
        return firstMessageMapper.selectFirstMessageById(id);
    }

    /**
     * 查询留言管理列表
     * 
     * @param firstMessage 留言管理
     * @return 留言管理
     */
    @Override
    public List<FirstMessage> selectFirstMessageList(FirstMessage firstMessage)
    {
        return firstMessageMapper.selectFirstMessageList(firstMessage);
    }

    /**
     * 新增留言管理
     * 
     * @param firstMessage 留言管理
     * @return 结果
     */
    @Override
    public int insertFirstMessage(FirstMessage firstMessage)
    {
        firstMessage.setCreateTime(DateUtils.getNowDate());
        return firstMessageMapper.insertFirstMessage(firstMessage);
    }

    /**
     * 修改留言管理
     * 
     * @param firstMessage 留言管理
     * @return 结果
     */
    @Override
    public int updateFirstMessage(FirstMessage firstMessage)
    {
        firstMessage.setUpdateTime(DateUtils.getNowDate());
        return firstMessageMapper.updateFirstMessage(firstMessage);
    }

    /**
     * 批量删除留言管理
     * 
     * @param ids 需要删除的留言管理主键
     * @return 结果
     */
    @Override
    public int deleteFirstMessageByIds(String ids)
    {
        return firstMessageMapper.deleteFirstMessageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除留言管理信息
     * 
     * @param id 留言管理主键
     * @return 结果
     */
    @Override
    public int deleteFirstMessageById(Long id)
    {
        return firstMessageMapper.deleteFirstMessageById(id);
    }

    /**
     * 获取首页留言列表
     * @return
     */
    @Override
    public List<FirstMessageVo> getFirstMessageList(){
        return firstMessageMapper.getFirstMessageList();
    }

    /**
     * 发布留言
     * @param dto
     */
    @Override
    public void saveLeaveMessage(LeaveMessageDto dto){
        if(dto.getUserId() == null){
            throw new RuntimeException("请先登录");
        }
        FirstMessage message = new FirstMessage();
        message.setUserId(dto.getUserId());
        message.setContent(dto.getContent());
        message.setState(State.ENABLE);
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());
        firstMessageMapper.saveLeaveMessage(message);
    }
}
